package misc

import (
	"dhfshop/controllers/base"
	"dhfshop/models/misc"
	"github.com/astaxie/beego/validation"
)

type PushMsgController struct {
	base.BaseController
}

func (this *PushMsgController) Get() {
	companyId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	offset := 0 //default is 0
	limit := 10 //default is 50
	v := struct {
		StoreId      int64  `form:"store_id"`
		StartCreated string `form:"start_created"`
		EndCreated   string `form:"end_created"`
		PageSize     int    `form:"page_size"`
		PageNo       int    `form:"page_no"`
		Status       string `form:"status"`
	}{PageSize: 10, PageNo: 1}
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Range(v.PageSize, 1, 1000, "page_size")
	valid.Range(v.PageNo, 1, 1000, "page_no")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	v.PageNo = v.PageNo - 1 //为了计算offset
	offset = v.PageNo * v.PageSize
	limit = v.PageSize
	list, cnt, err := misc.GetAllPushMsgQueues(companyId, v.StartCreated, v.EndCreated, v.Status, offset, limit)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		TotalCount int64                `json:"total_count"`
		PushMsgs   []*misc.PushMsgQueue `json:"push_msgs"`
	}{TotalCount: cnt, PushMsgs: list}
	this.Data["json"] = rsp
	this.ServeJSON()
}
